import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import junit.framework.TestCase;


public class DataBaseTest extends TestCase {

	public void testDataBase() {
		DataBase dB = new DataBase();
		assertTrue(dB.getUser("")==null);
		assertTrue(dB.getUser("richie")!=null);
		assertTrue(dB.getRestaurant("")==null);
		assertTrue(dB.getRestaurant("KFC")!=null);
	}

	public void testSetUsers() {
		DataBase dB = new DataBase();
		HashMap<String,User> users = new HashMap<String,User>();
		dB.setUsers(users);
		assertTrue(dB.getUsers().size()==0);
	}

	public void testGetUsers() {
		DataBase dB = new DataBase();
		assertTrue(dB.getUsers().size()!=0);
		assertTrue(dB.getUsers().containsKey("richie"));
	}

	public void testSetRestaurants() {
		DataBase dB = new DataBase();
		HashMap<String,Restaurant> r = new HashMap<String,Restaurant>();
		dB.setRestaurants(r);
		assertTrue(dB.getRestaurants().size()==0);
	}

	public void testGetRestaurants() {
		DataBase dB = new DataBase();
		assertTrue(dB.getRestaurants().size()!=0);
		assertTrue(dB.getRestaurants().containsKey("KFC"));
	}
	
	/*
	public void testReadUserDB() {
		fail("Not yet implemented");
	}

	public void testWriteUserDB() {
		fail("Not yet implemented");
	}

	public void testReadRestaurantDB() {
		fail("Not yet implemented");
	}

	public void testWriteRestaurantDB() {
		fail("Not yet implemented");
	}
	*/

	public void testPopulateUsers() {
		DataBase dB = new DataBase();
		assertTrue(dB.populateUsers().size()!=0);
		assertTrue(dB.populateUsers().containsKey("richie"));
	}

	public void testPopulateRestaurants() {
		DataBase dB = new DataBase();
		assertTrue(dB.populateRestaurants().size()!=0);
		assertTrue(dB.populateRestaurants().containsKey("KFC"));
	}

	public void testGetUser() {
		DataBase dB = new DataBase();
		assertTrue(dB.getUser("richie").getUsername().equals("richie"));
	}

	public void testAddUser() {
		DataBase dB = new DataBase();
		dB.addUser("testUser", new User("testUser","pass","type"));
		assertTrue(dB.getUser("testUser").getUsername().equals("testUser"));
	}

	public void testAddUserPreferences() {
		DataBase dB = new DataBase();
		UserPreferences uP = new UserPreferences("test","test","test",new Preferences());
		dB.addUserPreferences("richie", uP);
		assertTrue(dB.getUser("richie").getPreferences().lastElement().getName().equals("test"));
	}

	public void testGetRestaurant() {
		DataBase dB = new DataBase();
		assertTrue(dB.getRestaurant("KFC")!=null);
		assertTrue(dB.getRestaurant("")==null);
		assertTrue(dB.getRestaurant("kfc")==null);
	}

	public void testAddRestaurant() {
		DataBase dB = new DataBase();
		dB.addRestaurant("test", new Restaurant("test"));
		assertTrue(dB.getRestaurant("test").getName().equals("test"));
	}

	public void testGetRestaurantsByFoodType() {
		DataBase dB = new DataBase();
		Vector<String> test = dB.getRestaurantsByFoodType(Constants.BURGERS, "92122");
		for( String name : test ) {
			Restaurant r = dB.getRestaurant(name);
			assertTrue(r.getFoodType().equals(Constants.BURGERS));
			assertTrue(r.getZipCode().equals("92122"));
		}
	}

	public void testGetRestaurantsByZip() {
		DataBase dB = new DataBase();
		Vector<String> test = dB.getRestaurantsByZip("92122");
		for( String name : test ){
			Restaurant r = dB.getRestaurant(name);
			assertTrue(r.getZipCode().equals("92122"));
		}
	}

	public void testGetRestaurantsByPreferences() {
		DataBase dB = new DataBase();
		Preferences pref = new Preferences();
		pref.addCost(Constants.CHEAP);
		for( String name : dB.getRestaurantsByPreferences("cost", Constants.BURGERS, "92122", pref.getCost())) {
			Restaurant r = dB.getRestaurant(name);
			assertTrue(r.getPreferences().contains(pref));
		}
	}
	
	public void testGetRestaurantByName() {
		DataBase dB = new DataBase();
		HashMap<String,Restaurant> restaurants = dB.getRestaurants();

	    assertTrue(restaurants.keySet().containsAll(dB.getRestaurantsByName()));
	}

}